#!/usr/bin/env python
#
#  clientservertest.py  
#  Net Testing
#
#  Created by terry franguiadakis on 3/6/09.
#  Copyright (c) 2009 __MyCompanyName__. All rights reserved.
#
# ------------------------------------------------------------

import socket;
import time;

# ------------------------------------------------------------
class GameListener :
    def __init__(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
	self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1);
        self.sock.bind(('',8881));
        print "game_listener";
        self.sock.listen(5);
        self.start();
        
    def start(self):
        KeepListening = True
        while KeepListening:
            print "start"
            newSocket, address = self.sock.accept();
            print "connected from ", address;
            while KeepListening:
                print time.asctime()
                receiveData = newSocket.recv(8192);
                if not receiveData: 
                    print "break";
                    break;
                newSocket.sendall(receiveData);
                print receiveData;
                if receiveData == "quit":
                    KeepListening = False;
            newSocket.close();
            print "disconnected from", address;
        self.sock.close();
        print "Quitting.."
        
#------------------------------------------------------------
class NerdNerve :
    def __init__(self):
        self.sock = 0;

    def connect(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
	self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1);
        self.sock.connect(('', 8881))
        print "connected"

    def sendCommand(self, command):
        self.sock.sendall(command)
        response = self.sock.recv(8192)
        print "received:", response
        if response == "quit":
            self.close()

    def close(self):
        if self.sock:
            self.sock.close();
            self.sock = 0
        

# ----------------------------------------------------------------------------------
import cmd

class ConsoleHandler (cmd.Cmd):
	def __init__(self, nerdNerve):
            cmd.Cmd.__init__(self);
            self.intro = """---------------------------------------------\nWelcome to the NerdNerve\n-------------------------------------\n"""
            self.prompt = "()> "
            self.nerdNerve = nerdNerve
            self.isConnected = 0
		
	def do_connect(self, args):
            print "connecting...";
            try: 
                self.nerdNerve.connect()
                self.prompt = "(C)> "
                self.isConnected = 1
            except socket.error, (value, message):
                print "failed:", message
                self.prompt = "()> "
                self.isConnected = 0
        def help_connect(self):
            return "Connect to game"

        def do_close(self, args):
            print "closing connection";
            self.nerdNerve.close();
            self.prompt = "()> "
            self.isConnected = 0
        def help_close(self):
            return "Close connection to game."

        def default(self, args):
            try:
                if (self.isConnected):
                    self.nerdNerve.sendCommand(args)
                else:
                    print "cannot forward commands until connected."
            except socket.error, (value, message):
                print "failed ", message
                self.prompt = "()> "
                self.isConnected = 0

	def do_quit(self, args):
            print "quitting...";
            self.nerdNerve.close()
            return True
        def help_quit(self):
            return "Quit NerdNerve"

# ----------------------------------------------------------------------------------
# -- main 
# ----------------------------------------------------------------------------------



def main():
    ConsoleHandler(NerdNerve()).cmdloop();

if __name__ == '__main__':
    main()
        
    
        
    
